package util;

import org.bouncycastle.jce.provider.JDKDSASigner.stdDSA;

import play.Logger;
import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;

public class UnitConverter {
    
    private static final String DEFAULT_UNIT ="Sekunden";
    private static final int DEFAULT_DECIMAL_DIGITS = 3;

    public static int convertTFValuetoNormValue(TFSensorType sensorType, int decimalDigits, int TFValue) {
        int factor = (int) Math.pow(10, decimalDigits);
        int normValue = TFValue / factor;
        Logger.debug("convertTFValuetoNormValue: Type: %s DecimalDigits: %s factor: %s NormValue: %s TFValue: %s",
                sensorType, decimalDigits, factor, normValue, TFValue);
        return normValue;
    }

    public static int convertNormValuetoTFValue(TFSensorType sensorType, int decimalDigits, int normValue) {
        int factor = (int) Math.pow(10, decimalDigits);
        int TFValue = normValue * factor;
        Logger.debug("convertNormValuetoTFValue: Type: %s DecimalDigits: %s factor: %s NormValue: %s TFValue: %s",
                sensorType, decimalDigits, factor, normValue, TFValue);
        return TFValue;
    }

    public static String convertNormValuetoTFValue(TFSensorType sensorType, String normValue) {
        int value = convertNormValuetoTFValue(sensorType, sensorType.getDecimalDigits(), Integer.valueOf(normValue));
        return ""+value;
    }

    public static String convertTFValuetoNormValue(TFSensorType sensorType, String TFValue) {
        int value = convertTFValuetoNormValue(sensorType, sensorType.getDecimalDigits() ,Integer.valueOf(TFValue));
        return ""+value;
    }
    
    public static String convertNormValuetoTFValue(String propertyKey, String normValue) {
        int value = convertNormValuetoTFValue(findSensorTypebyKey(propertyKey),getDecimalDigits(propertyKey), Integer.valueOf(normValue));
        return ""+value;
    }
    
    public static String convertTFValuetoNormValue(String propertyKey, String TFValue) {
        int value = convertTFValuetoNormValue(findSensorTypebyKey(propertyKey),getDecimalDigits(propertyKey), Integer.valueOf(TFValue));
        return ""+value;
    }

    private static TFSensorType findSensorTypebyKey(String propertyKey){
        String name = "";
        TFSensorType sensorType = null;
        try {
            name = propertyKey.split("_m")[0];
            sensorType = TFSensorType.valueOf(name);
        } catch (Exception e) {
            Logger.warn("Did not find a SensorType: %s , retunrning null", name);
            return null;
        }
        return sensorType;
    }
    
    private static int getDecimalDigits(String propertyKey) {
        TFSensorType sensorType = findSensorTypebyKey(propertyKey);
        if (sensorType == null) {
            Logger.debug("No TFSensorType found, returning %s", DEFAULT_UNIT);
            return DEFAULT_DECIMAL_DIGITS;
        }
        return sensorType.getDecimalDigits();
    }
    
    public static String getUnit(String propertyKey) {
        TFSensorType sensorType = findSensorTypebyKey(propertyKey);
        if (sensorType == null) {
            Logger.debug("No TFSensorType found, returning %s", DEFAULT_UNIT);
            return DEFAULT_UNIT;
        }
        return sensorType.getUnit();
    }
}
